package com.ywjh.inter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;

import com.ywjh.inter.entity.DietPatientEntity;

public class DietPatientDAO {

	public int insert(Connection conn, DietPatientEntity entity) throws SQLException {
		int id=-1;
		PreparedStatement ps = conn.prepareStatement(
				"insert into diet_patient(diet_id,patient_id,add_time,status,hh,memo,type) values(?,?,?,?,?,?,?)",Statement.RETURN_GENERATED_KEYS);
		ps.setInt(1, entity.getDiet_id());
		ps.setInt(2, entity.getPatient_id());
		ps.setTimestamp(3, entity.getAdd_time());
		ps.setInt(4, entity.getStatus());
		ps.setString(5, entity.getHh());
		ps.setString(6, entity.getMemo());
		ps.setString(7, entity.getType());
		if (ps.executeUpdate() > 0) {
			ResultSet rs = ps.getGeneratedKeys();
			if (rs.next()) {
				id = rs.getInt(1);
			}
		}
		return id;
	}

	public boolean update(Connection conn, DietPatientEntity entity) throws SQLException {
		boolean b = false;
		PreparedStatement ps = conn.prepareStatement(
				"update diet_patient set diet_id=?,patient_id=?,add_time=?,status=?,hh=?,memo=?,type=? where id=?");
		ps.setInt(1, entity.getDiet_id());
		ps.setInt(2, entity.getPatient_id());
		ps.setTimestamp(3, entity.getAdd_time());
		ps.setInt(4, entity.getStatus());
		ps.setString(5, entity.getHh());
		ps.setString(6, entity.getMemo());
		ps.setString(7, entity.getType());
		ps.setInt(8, entity.getId());
		b = ps.executeUpdate() > 0 ? true : false;
		return b;
	}

	public DietPatientEntity findById(Connection conn, int id) throws SQLException {
		DietPatientEntity entity = null;
		PreparedStatement ps = conn.prepareStatement("select * from diet_patient where id =?");
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			entity = fill(rs);
		}
		return entity;
	}

	public List<DietPatientEntity> listByDietId(Connection conn, int diet_id,int patient_id) throws SQLException {
		List<DietPatientEntity> list = new LinkedList<>();
		PreparedStatement ps = conn.prepareStatement("select * from diet_patient where diet_id =? and patient_id=? order by add_time desc");
		ps.setInt(1, diet_id);
		ps.setInt(2, patient_id);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DietPatientEntity entity = fill(rs);
			list.add(entity);
		}
		return list;
	}

	private DietPatientEntity fill(ResultSet rs) throws SQLException {
		
		DietPatientEntity e = new DietPatientEntity();
		e.setId(rs.getInt("id"));
		e.setDiet_id(rs.getInt("diet_id"));
		e.setAdd_time(rs.getTimestamp("add_time"));
		e.setHh(rs.getString("hh"));
		e.setMemo(rs.getString("memo"));
		e.setType(rs.getString("type"));
		e.setPatient_id(rs.getInt("patient_id"));
		e.setStatus(rs.getInt("status"));
		return e;
	}

	public List<DietPatientEntity> listByDietId(Connection conn, int diet_id) throws SQLException {
		List<DietPatientEntity> list = new LinkedList<>();
		PreparedStatement ps = conn.prepareStatement("select * from diet_patient where diet_id =? order by status desc, add_time desc");
		ps.setInt(1, diet_id);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DietPatientEntity entity = fill(rs);
			list.add(entity);
		}
		return list;
	}

	public List<DietPatientEntity> listByPatientId(Connection conn, int patient_id) throws SQLException {
		List<DietPatientEntity> list = new LinkedList<>();
		PreparedStatement ps = conn.prepareStatement("select * from diet_patient where patient_id =? and status=1 order by status desc, add_time desc");
		ps.setInt(1, patient_id);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			DietPatientEntity entity = fill(rs);
			list.add(entity);
		}
		return list;
	}
	
	public DietPatientEntity findByTime(Connection conn,Timestamp timestamp, int patient_id, String type) throws SQLException {
		PreparedStatement ps = conn.prepareStatement("select * from diet_patient where patient_id =? and add_time=? and type=? and status=1 order by status desc, add_time desc");
		ps.setInt(1, patient_id);
		ps.setTimestamp(2, timestamp);
		ps.setString(3, type);
		ResultSet rs = ps.executeQuery();
		DietPatientEntity entity=null;
		if (rs.next()) {
			entity = fill(rs);
		}
		return entity;
	}


}
